package cc;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;

// 球队类，定义每个球队的属性和方法
class Team {
    String name; // 队名
    int matches; // 已比赛的场数
    int wins; // 赢的场数
    int draws; // 平的场数
    int losses; // 负的场数
    int goalsFor; // 进球数
    int goalsAgainst; // 失球数
    int points; // 积分

    // 构造函数
    public Team(String name) {
        this.name = name;
        this.matches = 0;
        this.wins = 0;
        this.draws = 0;
        this.losses = 0;
        this.goalsFor = 0;
        this.goalsAgainst = 0;
        this.points = 0;
    }

    // 记录比赛结果
    public void recordMatch(int goalsFor, int goalsAgainst) {
        this.matches++;
        this.goalsFor += goalsFor;
        this.goalsAgainst += goalsAgainst;
        if (goalsFor > goalsAgainst) {
            this.wins++;
            this.points += 3;
        } else if (goalsFor == goalsAgainst) {
            this.draws++;
            this.points += 1;
        } else {
            this.losses++;
        }
    }

    // 获取净胜球
    public int getGoalDifference() {
        return this.goalsFor - this.goalsAgainst;
    }
}

public class Test_five {
    // 比较器，用于排序
    static class TeamComparator implements Comparator<Team> {
        @Override
        public int compare(Team t1, Team t2) {
            if (t1.points != t2.points) {
                return t2.points - t1.points;
            } else if (t1.getGoalDifference() != t2.getGoalDifference()) {
                return t2.getGoalDifference() - t1.getGoalDifference();
            } else {
                return t2.goalsFor - t1.goalsFor;
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 初始化队伍信息
        Team[] teams = initializeTeams();

        // 输入本轮比赛结果
        System.out.println("输入本轮比赛结果：主队名，客队名，主队得分，客队得分");
        String homeTeamName = scanner.next();
        String awayTeamName = scanner.next();
        int homeScore = scanner.nextInt();
        int awayScore = scanner.nextInt();

        // 更新积分
        Team homeTeam = findTeamByName(teams, homeTeamName);
        Team awayTeam = findTeamByName(teams, awayTeamName);
        if (homeTeam != null && awayTeam != null) {
            homeTeam.recordMatch(homeScore, awayScore);
            awayTeam.recordMatch(awayScore, homeScore);

            // 排序并显示排名
            Arrays.sort(teams, new TeamComparator());
            displayTeams(teams);

        } else {
            System.out.println("输入的球队名称有误");
        }
        scanner.close();
    }

    // 初始化队伍信息
    public static Team[] initializeTeams() {
        // 示例初始化，实际应从文件或数据库加载
        return new Team[]{
            new Team("T1"),
            new Team("T2")
        };
    }

    // 根据队名查找队伍
    public static Team findTeamByName(Team[] teams, String name) {
        for (Team team : teams) {
            if (team.name.equals(name)) {
                return team;
            }
        }
        return null;
    }

    // 显示队伍信息
    public static void displayTeams(Team[] teams) {
        System.out.printf("%-11s %5s %5s %5s %5s %5s %5s %5s%n", "队名", "比赛场数", "赢", "平局", "输", "进球", "失球", "得分");
        for (Team team : teams) {
            System.out.printf("%-9s %7d %7d %6d %6d %6d %6d %6d%n", team.name, team.matches, team.wins, team.draws, team.losses, team.goalsFor, team.goalsAgainst, team.points);
        }
    }
}
